/*
 * BarraHerramientasDER.cpp
 *
 *  Created on: 27/04/2013
 *      Author: juan
 */

#include "gui/ButtonCreatorDER.h"
#include <gui/BarraDeHerramientas.h>
#include "gui/VisualComponents/der/VisualComponentEntity.h"
#include "gui/VisualComponents/der/VisualComponentAttribute.h"
#include "gui/VisualComponents/der/VisualComponentCompositeAttribute.h"
#include "gui/VisualComponents/der/VisualComponentRelation.h"
#include "gui/VisualComponents/der/VisualComponentForkVia.h"
#include <string>
#include "gui/Tabs.h"
#include "gui/SubWindow/DrawingAreaPlusPlus.h"
#include "gui/SubWindow/Workspace.h"
#include "utils/Settings.h"




ButtonCreatorDER::ButtonCreatorDER() : tabs(NULL), buttons(NULL), images(NULL){
}
ButtonCreatorDER::~ButtonCreatorDER() {}

void ButtonCreatorDER::inicializar(Tabs* tabs,std::vector<Gtk::Button*>& m_buttons,std::vector<Gtk::Image*>& m_images){
	std::string buttonNames[] = { "ENTIDAD", "RELACION", "ATRIBUTO", "AT_COMPUESTO", "JERARQUIA"};
	unsigned size = CANTIDAD_BOTONES_DER;
	this->tabs = tabs;

	std::string pathToImages = Settings::getInstance().getValue("ImagesPath");

	for ( unsigned i=0 ; i<size ; i++) {
		m_buttons.push_back(new Gtk::Button());
		m_images.push_back(new Gtk::Image(pathToImages + buttonNames[i] + ".png"));

		m_images[i]->show();
		m_buttons[i]->set_label(buttonNames[i]);
		m_buttons[i]->set_image_position(Gtk::POS_TOP);
		m_buttons[i]->set_image(*m_images[i]);
	}

	m_buttons[0]->signal_pressed().connect(sigc::mem_fun(this, &ButtonCreatorDER::createVisualComponent<VisualComponentEntity>));
	m_buttons[1]->signal_pressed().connect(sigc::mem_fun(this, &ButtonCreatorDER::createVisualComponent<VisualComponentRelation>));
	m_buttons[2]->signal_pressed().connect(sigc::mem_fun(this, &ButtonCreatorDER::createVisualComponent<VisualComponentAttribute>));
	m_buttons[3]->signal_pressed().connect(sigc::mem_fun(this, &ButtonCreatorDER::createVisualComponent<VisualComponentCompositeAttribute>));
	m_buttons[4]->signal_pressed().connect(sigc::mem_fun(this, &ButtonCreatorDER::createVisualComponent<VisualComponentForkVia>));
	buttons = &m_buttons;
	images = &m_images;
}

template< typename T > void ButtonCreatorDER::createVisualComponent(){
	Workspace* currentWorkspace = tabs->getCurrentWorkspace();
	if ( currentWorkspace == NULL)
		return;
	HandlerCreateComponent* handler = new HandlerCreateComponent(currentWorkspace, new T(currentWorkspace->getDiagram()));
	currentWorkspace->setHandler(handler);
	for( unsigned i=0 ; i<buttons->size() ; i++){
		(*buttons)[i]->get_display()->pointer_ungrab(0);
	}
}
